/*
 * Copyright 2013 JROSE
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jrose.service.webapp.exposer;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.jrose.core.util.OperatorContext;
import org.springframework.remoting.support.RemoteInvocation;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@RequiredArgsConstructor
public class ExtendedRemoteInvocation extends RemoteInvocation {
    @NonNull private final RemoteInvocation delegate;

    @Override
    public void setMethodName(String methodName) {
        delegate.setMethodName(methodName);
    }

    @Override
    public String getMethodName() {
        return delegate.getMethodName();
    }

    @Override
    public void setParameterTypes(Class[] parameterTypes) {
        delegate.setParameterTypes(parameterTypes);
    }

    @Override
    public Class[] getParameterTypes() {
        return delegate.getParameterTypes();
    }

    @Override
    public void setArguments(Object[] arguments) {
        delegate.setArguments(arguments);
    }

    @Override
    public Object[] getArguments() {
        return delegate.getArguments();
    }

    @Override
    public void addAttribute(String key, Serializable value) throws IllegalStateException {
        delegate.addAttribute(key, value);
    }

    @Override
    public Serializable getAttribute(String key) {
        return delegate.getAttribute(key);
    }

    @Override
    public void setAttributes(Map<String, Serializable> attributes) {
        delegate.setAttributes(attributes);
    }

    @Override
    public Map<String, Serializable> getAttributes() {
        return delegate.getAttributes();
    }

    @Override
    public Object invoke(Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        try {
            return delegate.invoke(targetObject);
        } finally {
            OperatorContext.getCurrentOperator().remove();
        }
    }

    @Override
    public String toString() {
        return delegate.toString();
    }
}
